package com.example.zzl.model.HomeFragment;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.example.zzl.model.bean.Home;
import com.example.zzl.model.cans.HomeUrl;
import com.example.zzl.utils.specification.VolleySingleton;
import com.example.zzl.viewmodel.HomeFragment.implement.IRecommendVm;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by ligg on 2017/2/9.
 */

public class RecommendModel implements Response.Listener<String> {


    private List<Object> list;
    private Home.HeadData headData;
    private Home.IconData iconData;
    private IRecommendVm callback;

    public RecommendModel(IRecommendVm callback) {
        this.callback = callback;
    }

    public void loadData() {
        StringRequest stringRequest = new StringRequest(HomeUrl.Recommend, this, VolleySingleton.volleySingleton);
        VolleySingleton.volleySingleton.getRequestQueue().add(stringRequest);
    }


    @Override
    public void onResponse(String response) {
//        Log.e("RecommendModel", "onResponse: " + response);
        list = new ArrayList<Object>();
        StringBuffer stringBuffer = new StringBuffer(response);
        stringBuffer.delete(0, stringBuffer.indexOf("{\"modelType\""));
        int temp = 0;
        int[] arr = new int[5];
        for (int i = 0; i < 5; i++) {
            temp = stringBuffer.indexOf("{\"modelType\"", temp);
            arr[i] = temp;
            temp += 20;
//            Log.e("RecommendModel", "onResponse: " + temp);
        }
        Gson gson = new Gson();
        String substring = stringBuffer.substring(arr[0], arr[1] - 1);
         headData = gson.fromJson(substring, Home.HeadData.class);
//        Log.e("RecommendModel", "onResponse: " + headData.toString());
        String substring1 = stringBuffer.substring(arr[1], arr[2] - 1);
         iconData = gson.fromJson(substring1, Home.IconData.class);
//        Log.e("RecommendModel", "onResponse: " + iconData);
        String substring2 =

                stringBuffer.substring(arr[2], arr[3] - 1);
        Home.OrderCommodity orderCommodity = gson.fromJson(substring2, Home.OrderCommodity.class);
//        Log.e("RecommendModel", "onResponse: " + orderCommodity);
        String substring3 = stringBuffer.substring(arr[3], arr[4] - 1);
        Home.Special special = gson.fromJson(substring3, Home.Special.class);
//        Log.e("RecommendModel", "onResponse: " + special);
        String substring4 = stringBuffer.substring(arr[4], stringBuffer.length() -2);
        Home.BrandExclusive brandExclusive = gson.fromJson(substring4, Home.BrandExclusive.class);
//        Log.e("RecommendModel", "onResponse: " + brandExclusive);
        Collections.addAll(list, orderCommodity, special, brandExclusive);
        callback.loadFinished();
    }

    public List<Object> getList() {
        return list;
    }

    public Home.HeadData getHeadData() {
        return headData;
    }

    public Home.IconData getIconData() {
        return iconData;
    }
}
